package com.team.abs.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import com.team.abs.entity.simple_user;
import com.team.abs.service.*;
import com.team.abs.service.baiduapi.*;
import com.team.abs.service.check.check;


import net.sf.json.JSONObject;
@Controller
public class user_login_controller {
	@Autowired
	private user_status_service user_status_service;
	@Autowired
	private user_login_service user_login_service;
	@Autowired
	private black_white_service black_white_service;
	@GetMapping("/register")
	public String register(){
		return "simple_user_register";
	}
	@GetMapping("/login")
	public String login(){
		return "login";
	}
	//简易的注册功能 实际直接导入数据库
	@ResponseBody
	@RequestMapping(value="/manInsert_api",method=RequestMethod.POST)
	public String manInsert(
			@RequestParam(value="user_uid",required=true)String user_uid,
			@RequestParam(value="user_password",required=true)String user_password,
			@RequestParam(value="user_name",required=true)String user_name,
			@RequestParam(value="user_school_id",required=true)String user_school_id,
			@RequestParam(value="user_qq",required=true)String user_qq,
			@RequestParam(value="user_tel",required=true)String user_tel,
			@RequestParam("file")MultipartFile file ) throws IllegalStateException, IOException {
		JSONObject result=new JSONObject();
		simple_user user=new simple_user(user_uid, user_password, user_name, user_school_id, user_qq, user_tel);
		//判断数据规范
		String res=check.check(user);
		if(!res.equals("")) {
			result.put("status", res);
			return result.toString();
		}
		//当存在用户就退出
		if(user_status_service.search_simple_user(user_uid)>0) {
			result.put("status", "001");
			return result.toString();
		}
		
		String path="/img/"+user_uid+".png";
		file.transferTo(new File("D:"+path));
		//上传到百度云
		faceapi.faceadd("D:"+path,user_uid,user_name);
		if(user_status_service.insert_simple_user(user)>0) 	
		result.put("status", "success");
		else result.put("status", "002");

		return result.toString();
	} 
	
		@ResponseBody
		@RequestMapping(value="/simple_user_login_api",method=RequestMethod.POST)
		public String simple_user_login(
				@RequestParam(value="user_uid",required=true)String user_uid,
				@RequestParam(value="user_password",required=true)String user_password){
			JSONObject result=new JSONObject();
			String st="";
			if(user_login_service.user_login(user_uid, user_password)==0) {
				st+="没有该用户或密码不对";
				result.put("status", st);return result.toString();}
			if(black_white_service.search_black_man(user_uid, 0)!=0) {
				st+="该用户被禁止登录";
			result.put("status", st);
			return result.toString();}
			 result.put("status", "success");

			return result.toString();
		} 
		@ResponseBody
		@RequestMapping(value="/simple_user_power_api",method=RequestMethod.POST)
		public String simple_user_power(@RequestParam(value="user_uid",required=true)String user_uid){
			JSONObject result=new JSONObject();
			if(black_white_service.search_black_man(user_uid, 0)>0)
			result.put("status", "fail");
			else result.put("status", "success");

			return result.toString();
		} 
		//人脸登录
		@ResponseBody
		@RequestMapping(value="/simple_user_face_login_api",method=RequestMethod.POST)
		public String simple_user_face_login(){
			JSONObject result=new JSONObject();
			String Uid;
			//将存储的图片用于人脸比对
			System.out.println("fail");
			if((Uid=faceapi.facesearch("D:\\img\\target.png"))==null)
			{
			System.out.println("fail");
			return result.toString();}
			result.put("status", Uid);
			//没有在黑名单中 就放开
			if(black_white_service.search_black_man(Uid, 0)==0)
			result.put("status", Uid);
			else {result.put("status", "error");
				result.put("error", "在黑名单中无法登录");}
			return result.toString();
		} 
}
